package org.example.exampreparationtreasure.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.exampreparationtreasure.service.QuestionSelectorService;
import org.example.exampreparationtreasure.mapper.QuestionMapper;
import org.example.exampreparationtreasure.model.entity.ExamPaperConfig;
import org.example.exampreparationtreasure.model.entity.Question;
import org.example.exampreparationtreasure.model.entity.PracticeMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 随机练习题目选择器
 * 随机抽取题目进行练习
 */
@Service
public class RandomQuestionSelectorServiceImpl implements QuestionSelectorService{
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> selectQuestions(ExamPaperConfig config, Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        // 用试卷配置的难度筛选题目
        queryWrapper.eq("difficulty", getDifficultyLevel(config.getInitialDifficulty()));

        List<Question> allQuestions = questionMapper.selectList(queryWrapper);

        if (allQuestions.size() <= config.getQuestionCount()) {
            return allQuestions;
        }

        Collections.shuffle(allQuestions);
        return allQuestions.subList(0, config.getQuestionCount());
    }


    private String getDifficultyLevel(Integer initialDifficulty) {
        switch (initialDifficulty) {
            case 1: return "易";
            case 2: return "中";
            case 3: return "难";
            default: return "易";
        }
    }
}
